<html>
<head><meta charset="utf-8"><title>proc-macro no ast-node · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html">proc-macro no ast-node</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220103300"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220103300" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edwin Cheng <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220103300">(Dec 16 2020 at 11:17)</a>:</h4>
<p>I opened PR #<a href="https://github.com/rust-analyzer/rust-analyzer/pull/6901">6901</a> which temp fix some panics due to current design :</p>
<p>We don't have <code>ast-node</code> for proc-macro such that <code>HasSource::source</code> fail.<br>
So how would we handle this problem properly ?</p>



<a name="220104492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220104492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220104492">(Dec 16 2020 at 11:31)</a>:</h4>
<p>Oh</p>



<a name="220104504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220104504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220104504">(Dec 16 2020 at 11:31)</a>:</h4>
<p>oh oh oh oh no</p>



<a name="220104967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220104967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220104967">(Dec 16 2020 at 11:37)</a>:</h4>
<p>This seems to be one of that cases where innocuous-looking design decision early on would lead to a lot of pain down the road. </p>
<p>I think, fundamentally, <code>HasSource</code> needs to return an <code>Option</code>, for two reasons. </p>
<ul>
<li>First, there are entities which genuinely don't have source, like proc-macros or primitive types. Those I think actually should be fixed on the type level. For example, the thing which currently implements <code>HasSource</code> and can represent proc-macros should be split into separate entities for proceduran and declarative macros, and only declarative macros should implement <code>HasSource</code>. </li>
<li>Second, and this is a deeper problem, things from <code>.rlib</code>s don't have source by virtue of being in an <code>.rlib</code>. This requres to change HasSource to return an Option.</li>
</ul>



<a name="220105078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220105078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220105078">(Dec 16 2020 at 11:38)</a>:</h4>
<p><code>.rlib</code>'s do contains <code>Span</code>'s from which you could get the original source in most cases.</p>



<a name="220105091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220105091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220105091">(Dec 16 2020 at 11:39)</a>:</h4>
<p><span class="user-mention" data-user-id="216201">@Edwin Cheng</span> how hard would it be to split <code>hir::MacroDef</code> into separate <code>hir::ProcMacro</code> and <code>hir::DeclMacro</code>, such that  only the latter implements <code>HasSource</code>?</p>



<a name="220105257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220105257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220105257">(Dec 16 2020 at 11:40)</a>:</h4>
<p>Technically <code>HasSource</code> could point you to the function marked as <code>#[proc_macro]</code>, since they now go through hir_def name resolution</p>



<a name="220105284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220105284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220105284">(Dec 16 2020 at 11:41)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> that's true, but there's a difference between "source is an immanent property of an object" and "you can get source in 99.99% of the time". Although the user-visible experience would be almost identiical, the two situations need fundamentally different code in the implementation. </p>
<p>And I think it is true that source is not an inherent property of the things we have in code-model, there can be various synthetic things which behave as if they were real, but lack the source nontheless.</p>



<a name="220105555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220105555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edwin Cheng <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220105555">(Dec 16 2020 at 11:45)</a>:</h4>
<p>Not that hard i think , but <span class="user-mention" data-user-id="211727">@Jonas Schievink</span> is refactoring for macro 2.0. I would prefer doing it after that</p>



<a name="220105827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220105827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edwin Cheng <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220105827">(Dec 16 2020 at 11:48)</a>:</h4>
<blockquote>
<p>Technically <code>HasSource</code> could point you to the function marked as <code>#[proc_macro]</code>, since they now go through hir_def name resolution</p>
</blockquote>
<p>Yes, and goto def in following cases works for now:</p>
<div class="codehilite"><pre><span></span><code>use foo; // foo is proc macro
</code></pre></div>



<a name="220107428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220107428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220107428">(Dec 16 2020 at 12:08)</a>:</h4>
<p>But, as usual, the ground truth is the <code>.so</code> with the macro, and not the source COde</p>



<a name="220114656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/proc-macro%20no%20ast-node/near/220114656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/proc-macro.20no.20ast-node.html#220114656">(Dec 16 2020 at 13:28)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133169">matklad</span> <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/proc-macro.20no.20ast-node/near/220105284">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> that's true, but there's a difference between "source is an immanent property of an object" and "you can get source in 99.99% of the time".</p>
</blockquote>
<p>I learned a new word today! <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>